package actions;

import java.sql.SQLException;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.RowSet;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import db.DbException;
//import forms.StudentForm;
import model.AdminModel;

public class AdminGetByReviewersAction extends Action {

	private static final boolean DEBUG = true;

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws DbException {
		
		String path = mapping.getPath();
		
		if (DEBUG) {
			System.err.println("AdminGetByReviewersAction: " + path);
		}
		
		// Retrieve all reviewers
		RowSet reviewerSet = AdminModel.getAllReviewers();
		Vector<String> reviewers = new Vector<String>();
		// Store all the reviewers into a vector
		try {
		while(reviewerSet.next())
		{
			reviewers.add(reviewerSet.getString(1));
			System.err.println("Reviewer: " + reviewerSet.getString(1));
			
		} } catch (SQLException e) {
			throw new DbException(e);
		}
		
		// Store the Vector in the request scope
		request.setAttribute("reviewers", reviewers);
		
		Vector<String> finished = new Vector<String>();
		Vector<String> unfinished = new Vector<String>();

		for(int i = 0; i < reviewers.size(); i++)
		{
			int numFinished = AdminModel.getFinishedAppReviews(reviewers.get(i));
			int numUnfinished = AdminModel.getUnfinishedAppReviews(reviewers.get(i));
			// flag in case you don't have any rowset to iterate through
				
			finished.add(Integer.toString(numFinished));
			unfinished.add(Integer.toString(numUnfinished));
				
			System.err.println("Added to vector finished for user: " + reviewers.get(i) + 
					" --> " + numFinished);	
			System.err.println("Added to vector unfinished for user: " + reviewers.get(i) + 
					" --> " + numUnfinished);	

		} 

		// Store the Vector in the request scope
		request.setAttribute("finished", finished);
		
		// Store the Vector in the request scope
		request.setAttribute("unfinished", unfinished);

		
		return mapping.findForward("success");
	}
}